package com.dg11185.nearshop.net.request;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.LightingColorFilter;
import android.util.Log;

import com.dg11185.libs.network.http.AsyncHttpClient;
import com.dg11185.libs.network.http.JsonHttpResponseHandler;
import com.dg11185.libs.network.http.RequestParams;
import com.dg11185.libs.utils.MD5.MD5Utils;
import com.dg11185.nearshop.app.Constants;
import com.dg11185.nearshop.app.ViewUtils;

/**
 * 上传用户头像
 * 
 * @author wanliang527
 * @date 2014-07-08
 * 
 */
public class UploadUserIcon {

    private File imgFile;
    private String userId;
    private String chshToken;
    private HashMap<String, String> mParams;

    private String responseStr = "";
    private String status = "";
    private String minPhoto = "";

    public UploadUserIcon(File imgFile, String userId) {
        this.imgFile = imgFile;
        this.userId = userId;
        this.chshToken = Constants.TOKEN;
        mParams = new HashMap<String, String>();
        mParams.put("userId", userId);
        mParams.put("chshToken", chshToken);
    }

    public void upload() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                netAction();
            }
        }).start();

    }

    private void netAction() {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpRequest = new HttpPost(Constants.ADDRESS
                + Constants.upload);
        MultipartEntity mpEntity = new MultipartEntity();
        try {
            mpEntity.addPart("imgFile", new FileBody(imgFile));
            mpEntity.addPart("userId",
                    new StringBody(userId, Charset.forName("UTF-8")));
            mpEntity.addPart("chshToken",
                    new StringBody(chshToken, Charset.forName("UTF-8")));
            mpEntity.addPart(
                    "chshSign",
                    new StringBody(MD5Utils.getMD5Str(mParams,
                            Constants.MD5_key), Charset.forName("UTF-8")));
            httpRequest.setEntity(mpEntity);
            HttpResponse httpResponse = httpClient.execute(httpRequest);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            responseStr = EntityUtils.toString(httpResponse.getEntity(),
                    "UTF-8");
            if (statusCode == 200) {
                JSONObject jsonObject = new JSONObject(responseStr);
                status = jsonObject.getString("status");
                minPhoto = jsonObject.getString("imgUrl");
                if (mResponse != null) {
                    mResponse.onSuccess(responseStr, status, minPhoto);
                }
            } else {
                if (mResponse != null) {
                    mResponse.onFailed(statusCode);
                }

            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private UploadUserIconResponse mResponse;

    public void setUploadResponse(UploadUserIconResponse response) {
        this.mResponse = response;
    }

    public interface UploadUserIconResponse {
        public void onSuccess(String responseStr, String status, String minPhoto);

        public void onFailed(int resultCode);
    }

    public String getMinPhoto() {
        return minPhoto;
    }

}
